#include <bits/stdc++.h>
// 2025/01/28
// tag: 
// Author: Zhang Muen
using namespace std;

int cnt, factor[50001];

void get_factor(int b1){
    cnt = 0;
    for (int i = 1; i * i <= b1; i++)
        if (b1 % i == 0){
            factor[cnt++] = i;
            if (i != b1 / i)
                factor[cnt++] = b1 / i;
        }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int a0, a1, b0, b1, ans = 0;
        cin >> a0 >> a1 >> b0 >> b1;
        int k1 = a0 / a1, k2 = b1 / b0;
        get_factor(b1);
        for (int j = 0; j < cnt; j++){
            int x = factor[j];
            if (x % a1 == 0 && __gcd(x / a1, k1) == 1 && __gcd(b1 / x, k2) == 1)
                ans++;
        }
        cout << ans << endl;
    }
    return 0;
}